package org.edu.ccnu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "serverController")
public class Server {

	private int flag=0;//全局变量，教师控制所有客户端的开关，默认为关闭
	/**
	 * 接收教师端的请求
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "serverControl")
	public void control(HttpServletRequest request,HttpServletResponse response){

    	String type=request.getParameter("type");//接收来自教师页面的控制信息
    	if(type.equals("0")){//如果为1就代表教师发出请求，让开关flag变为1，如果开关为1，
    		                 //则所有页面都要跳转到指定页面
    		flag=0;
    	}else{
    		flag=1;
    	}
    	System.out.println(flag);		
	}
	
	/**
	 * 学生端页面不断请求此服务，获得flag开关的信息
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "message")
	@ResponseBody
	public String message(HttpServletResponse response){
		String str="{\"flag\":"+flag+"}";
		System.out.println(str);
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		return str;
	}
	
}
